package com.example.test.advice.第三章_类_对象_方法.建议45_覆写equals方法时不要识别不出自己;

import java.util.ArrayList;
import java.util.List;

public class EqualsClient {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        Person p1 = new Person("张三");
        Person p2 = new Person("张三 ");
        personList.add(p1);
        personList.add(p2);
        System.out.println("列表中是否包含张三："+ personList.contains(p1));//true
        System.out.println("列表中是否包含张三："+personList.contains(p2));//false
    }
}

class Person{
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //违反了自反性原则：x.equqls(x) true
    @Override
    public boolean equals(Object o) {
       if(o instanceof Person){
           Person p = (Person) o;
           return name.equalsIgnoreCase(p.getName().trim());//覆写的equals方法有问题
       }
       return false;
    }

}
